<?php
/**
* 
*/
class StaffReportModel extends MY_Model{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'user_appoint';
	}


	//折线图数据
	public function coach_appoint_data($dtnum = 90)
	{
		$date_arr = array();
		$appoint_arr = array();

		if (!is_numeric($dtnum)) 
		{
			//处理日期
			$d1 = substr($dtnum,0,10);
			$d2 = substr($dtnum,-10);
			$dlist = $this->getDateFromRange($d1,$d2);

			for ($i=0; $i < count($dlist); $i++) 
			{
				$date_arr[] = substr($dlist[$i],-5);

				$start = date('Y-m-d 00:00:00',strtotime($dlist[$i]));
				$end = date('Y-m-d 00:00:00',strtotime($dlist[$i])+86400);

				$query = $this->db->query("SELECT COUNT(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND status = 1");
				$res = $query->row_array();

				$appoint_arr[] = $res['appoint_num'] ? $res['appoint_num'] : 0;
			}
		}
		else
		{
			for ($i=$dtnum; $i > 0; $i--) 
			{
				$date_arr[] = date('m-d',strtotime('-'.$i.' day'));

				$start = date('Y-m-d 00:00:00',strtotime('-'.$i.' day'));
				$end = date('Y-m-d 00:00:00',strtotime('-'.($i-1).' day'));

				$query = $this->db->query("SELECT COUNT(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND status = 1");
				$res = $query->row_array();

				$appoint_arr[] = $res['appoint_num'] ? $res['appoint_num'] : 0;
			}

		}

		$data['date_list'] = $date_arr;
		$data['appoint_num'] = $appoint_arr;

		return $data;

	}


	/**
	 * 获取指定日期段内每一天的日期
	 * @param  Date  $startdate 开始日期
	 * @param  Date  $enddate   结束日期
	 * @return Array
	 */
	function getDateFromRange($startdate, $enddate)
	{
	    $stimestamp = strtotime($startdate);
	    $etimestamp = strtotime($enddate);

	    // 计算日期段内有多少天
	    $days = ($etimestamp-$stimestamp)/86400+1;

	    // 保存每天日期
	    $date = array();
	    for($i=0; $i<$days; $i++){
	        $date[] = date('Y-m-d', $stimestamp+(86400*$i));
	    }

	    return $date;
	}


	//
	public function get_appoint($vid,$cid,$sdate,$page=1,$size=50){
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');

		if (!empty($sdate)) {
			//处理日期
			$d1 = substr($sdate,0,10);
			$d2 = substr($sdate,-10);

			$start = date('Y-m-d H:i:s',strtotime($d1));
			$end = date('Y-m-d H:i:s', strtotime($d2)+86400);
		}

		$where = "ap.addtime >= unix_timestamp('$start') AND ap.addtime <= unix_timestamp('$end') AND ap.status = 1";

		if (intval($vid) > 0) {
			$where .= " AND ap.shop_id =".$vid;
		}

		if (intval($cid) > 0) {
			$where .= " AND ap.mid =".$cid;
		}
		$sql = "SELECT ap.addtime, ap.sdate, ap.stime, ap.num, co.nickname, c.name as course_name, ca.card_name, ca.price as card_price , ca.course_num, u.nickname as user_name FROM nmp_user_appoint as ap 
			LEFT JOIN nmp_coach as co ON ap.mid = co.id 
			JOIN nmp_course as c ON ap.cid = c.id 
			JOIN nmp_user_card as uc ON ap.card_id = uc.id 
			JOIN nmp_course_card as ca ON uc.card_id = ca.id 
			JOIN nmp_user as u ON ap.user_id = u.id  
			WHERE ".$where." ORDER BY ap.addtime DESC LIMIT ".($page-1)*$size.",".$size;
			

			
		$query = $this->db->query($sql);
		$data = $query->result_array();

		$total = 0;
		foreach ($data as $key => $value) {
			$data[$key]['addtime'] = date('Y-m-d', $value['addtime']);
			
			$data[$key]['price'] = toFloat($value['card_price'] / $value['course_num']);
			
			$total += $data[$key]['price'];
		}

		$sql = "select COUNT(*)  as num from nmp_user_appoint as ap 
			LEFT JOIN nmp_coach as co ON ap.mid = co.id 
			JOIN nmp_course as c ON ap.cid = c.id 
			JOIN nmp_user_card as uc ON ap.card_id = uc.id 
			JOIN nmp_course_card as ca ON uc.card_id = ca.id 
			JOIN nmp_user as u ON ap.user_id = u.id 
			WHERE {$where} limit 1";
			
		$query = $this->db->query($sql);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		
		
		
		
		$result = array(
			'list'=>$data,
			'count'=>$count, 
			'pages'=>$pages,
			'total'=>toFloat($total)
		);
		
		
		
		return $result;
	}

	function get_data($arr=array()){
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');

		if (isset($arr['dates']) && !empty($arr['dates'])) {
			//处理日期
			$d1 = substr($arr['dates'],0,10);
			$d2 = substr($arr['dates'],-10);

			$start = date('Y-m-d H:i:s',strtotime($d1));
			$end = date('Y-m-d H:i:s', strtotime($d2)+86400);
		}

		$where = "ap.addtime >= unix_timestamp('$start') AND ap.addtime <= unix_timestamp('$end') AND ap.status = 1";

		if (isset($arr['venue_id']) && intval($arr['venue_id'])>0) {
			$where .= " AND ap.shop_id ='{$arr['venue_id']}'";
		}

		if (isset($arr['coach_id']) && intval($arr['coach_id'])>0) {
			$where .= " AND ap.mid ='{$arr['coach_id']}'";
		}
		
		$sql = "SELECT ap.addtime, ap.sdate, ap.stime, ap.num, co.nickname, c.name as course_name, ca.card_name, ca.price as card_price , ca.course_num, u.nickname as user_name FROM nmp_user_appoint as ap 
			LEFT JOIN nmp_coach as co ON ap.mid = co.id 
			JOIN nmp_course as c ON ap.cid = c.id 
			JOIN nmp_user_card as uc ON ap.card_id = uc.id 
			JOIN nmp_course_card as ca ON uc.card_id = ca.id 
			JOIN nmp_user as u ON ap.user_id = u.id  
			WHERE ".$where." ORDER BY ap.addtime DESC LIMIT 1000";
			
		$query = $this->db->query($sql);
		$list = $query->result_array();

		foreach ($list as $key => $value) {
			$list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
			
			$list[$key]['price'] = toFloat($value['card_price'] / $value['course_num']);
			
			
		}
		
		
		return $list;
	}



}





?>